<?php
namespace mz;
use think\Config;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class Sms{
	private static $client;
	public static __construct(){
		$accessKeyId=Config::get('alisms.accessKeyId');
		$accessSecret=Config::get('alisms.accessSecret');
		AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret)->regionId('cn-hangzhou')->asDefaultClient();
		$self::client = AlibabaCloud::rpc()->product('Dysmsapi')->version('2017-05-25');
	}
	/**
	 * 发送短信
	 * @param string phoneNumber发送手机号,多个手机号用英语逗号隔开
	 * @param int templateCode模板ID
	 *
	 * @return boolean true 发送成功/失败
	 * @author 
	 **/
	public static function send($phoneNumber,$templateCode){
		
		try{
			$result=$sefl::client->action('SendSms')->method('POST')->host('dysmsapi.aliyuncs.com')->options([
			'query' => [
                     'RegionId' => "cn-hangzhou",
                     'PhoneNumbers' => $phoneNumber,
                     'SignName' => "明志",
                     'TemplateCode' => $templateCode,
                	],
		])->request();
		$result=$result->toArray();
		if($result['message']=='OK'){
			return true;
		}else{
			return false;
		}
		}catch(ClientException $e){
			 echo $e->getErrorMessage() . PHP_EOL;
		}catch(ServerException $e){
			echo $e->getErrorMessage() . PHP_EOL;
		}
	}
	/**
	 * 增加签名
	 * @param string signName签名名称
	 * @return boolean true 成功/失败
	 * @author 
	 **/
	public static function addSign($signName){
		
		try{
			$result=$sefl::client->action('AddSmsSign')->method('POST')->host('dysmsapi.aliyuncs.com')->options([
			'query' => [
                     'RegionId' => "cn-hangzhou",
                     'SignName' => $signName,
                     'SignSource' => "0",
                     'Remark' => '发送验证码',
                	],
		])->request();
		return true;
		}catch(ClientException $e){
			 echo $e->getErrorMessage() . PHP_EOL;
		}catch(ServerException $e){
			echo $e->getErrorMessage() . PHP_EOL;
		}
	}
	/**
	 * 增加模板
	 * @param string templateType模板类型
	 * @param string templateName模板类型
	 * @param string templateContent模板内容
	 * @param string remark备注
	 * @return boolean true 成功/失败
	 * @author 
	 **/
	public static function addTemplate($templateType,$templateName,$templateContent,$Remark){
		
		try{
			$result=$sefl::client->action('AddSmsTemplate')->method('POST')->host('dysmsapi.aliyuncs.com')->options([
			'query' => [
                     'RegionId' => "cn-hangzhou",
                     'TemplateType' => $TemplateType,
                     'TemplateName' => $TemplateName,
                     'TemplateContent'=>$TemplateContent,
                     'Remark' => $remark,
                	],
		])->request();
		return true;
		}catch(ClientException $e){
			 echo $e->getErrorMessage() . PHP_EOL;
		}catch(ServerException $e){
			echo $e->getErrorMessage() . PHP_EOL;
		}
	}
}